layui.define(function (exports) {

  var helper = {
    hasKey: function (obj, key) {
      return obj.hasOwnProperty(key)
    }
    , emptyKey: function (obj, key) {
      return !obj.hasOwnProperty(key) || (obj[key] === '' || obj[key] === null);
    }
    , hasKeyFnt: function (obj, key, callback) {
      if (obj.hasOwnProperty(key)) callback(obj, key)
    }
  };

  helper.getValue = function (obj, key, defaultValue) {
    if (this.hasKey(obj, key)) return obj[key];
    else return defaultValue;
  };

  helper.removeValue = function (obj, key, defaultValue) {
    if (this.hasKey(obj, key)) {
      var v = obj[key];
      delete obj[key];
      return v;
    } else {
      return defaultValue;
    }
  };

  helper.getCondition = function (condition, data) {
    var where = {};
    layui.each(condition, function (k, v) {
      if (v.indexOf(':') === -1) {
        where[k] = v
      } else if (v.indexOf(':') === 0) {
        where[k] = data[v.substr(1)]
      }
    });
    return where
  };

  helper.urlEncode = function (param) {
    if (param == null) return '';
    var arr = [];
    layui.each(param, function (k, v) {
      arr.push(k + '=' + v)
    });
    return arr.join('&');
  };


  var tableConfig = function () {
    this.headers = {AOP: 1};
    this.where = {Condition: {}, Sort: {}};
    this.page = {};
  };

  tableConfig.prototype.setProperty = function (k, v) {
    this[k] = v;
  };

  tableConfig.prototype.setHeader = function (key, value) {
    this.headers[key] = value
  };

  tableConfig.prototype.setCondition = function (key, value) {
    this.where.Condition[key] = value
  };

  tableConfig.prototype.setConditions = function (conditions) {
    this.where.Condition = conditions
  };

  tableConfig.prototype.setSort = function (obj) {
    this.where.Sort = obj
  };

  tableConfig.prototype.setSortValue = function (key, value) {
    this.where.Sort[key] = value
  };

  tableConfig.prototype.setPage = function (key, value) {
    this.page[key] = value;
  };

  helper.getTableConfig = function () {
    return new tableConfig();
  };

  exports('helper', helper)

});